/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package formulagraph;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author soler4888
 */
public class Canvas {

    private List<Formula> formulas;
    private List<EntradaLeyenda> entradasLeyenda;
    private double x;
    private double y;
    private double width;
    private double height;
    private Ejes ejes;
    private Texto tituloLeyenda;
    private int posicion;
    private int indice;


    public Canvas(double x, double y, double width, double height) {
        this.formulas = new ArrayList<Formula>();
        this.entradasLeyenda = new ArrayList<EntradaLeyenda>();
        this.tituloLeyenda = new Texto("Leyenda", 0, 20, 14);
        this.posicion = 60;
        this.indice = 1;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.ejes = null;

    }

    public void addFormula(Formula formula) {
        formulas.add(formula);
        entradasLeyenda.add(new EntradaLeyenda(0, posicion, "Función Y" + indice, formula.getLinea()));
        posicion += 20;
        indice++;
    }

    public void setEjes(Ejes ejes) {
        this.ejes = ejes;
    }

    public double getHeight() {
        return height;
    }

    public double getWidth() {
        return width;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public String dibujar() {
        StringBuilder builder = new StringBuilder();

        // Canvas
        builder.append("<svg ");
        // Class
        builder.append("class=\"canvas\" ");
        // Reference
        builder.append("xmlns=\"http://www.w3.org/2000/svg\" ");
        // viewBox property
        builder.append("viewBox=\"");
        builder.append(x).append(" ").append(-y).append(" ");
        builder.append(width).append(" ").append(height);
        builder.append("\" ");
        // preserveAspectRatio property
        builder.append("preserveAspectRatio=\"none\" ");
        // version property
        builder.append("version=\"1.1\" ");
        builder.append(">\n");

        // Ejes
        if (ejes != null) {
            builder.append(ejes.dibujar()).append("\n");
        }

        // Formulas
        Iterator<Formula> it = formulas.iterator();
        while (it.hasNext()) {
            Formula f = it.next();
            builder.append(f.dibujar()).append("\n");
        }

        // Close canvas
        builder.append("</svg>");

        // Leyenda
        builder.append("<svg ");
        // Class
        builder.append("class=\"legend\" ");
        // Titulo
        builder.append("title=\"Leyenda\"");
        // Reference
        builder.append("xmlns=\"http://www.w3.org/2000/svg\" ");
        // viewBox property
        builder.append("viewBox=\"");
        builder.append(0).append(" ").append(0).append(" ");
        builder.append(200).append(" ").append(200);
        builder.append("\" ");
        // version property
        builder.append("version=\"1.1\" ");

        builder.append(">\n");
        builder.append(tituloLeyenda.dibujar());
        builder.append("<line stroke-dasharray=\"16,8,4,8\" stroke-linecap=\"round\" stroke-width=\"0.8\" stroke=\"black\" y2=\"40.0\" x2=\"55.0\" y1=\"40.0\" x1=\"0.0\" />\n");
        builder.append("<text font-family=\"Verdana\" font-size=\"12\" y=\"44.0\" x=\"65.0\">Ejes</text>\n");

        for (EntradaLeyenda entrada : entradasLeyenda) {
            entrada.getLinea().getLinea().setAnchuraLinea(1d);
            builder.append(entrada.dibujar());
        }
        // Close canvas
        builder.append("</svg>");

        return builder.toString();
    }
}
